﻿namespace BadHabit.WPFTools.Converters
{
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;

    public class EnumBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int parameterInt;

            if (!Int32.TryParse(parameter.ToString(), out parameterInt))
            {
                return DependencyProperty.UnsetValue;
            }

            int valueInt;
            if (!Int32.TryParse(value.ToString(), out valueInt))
            {
                return DependencyProperty.UnsetValue;
            }

            return valueInt == parameterInt;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is bool))
            {
                return DependencyProperty.UnsetValue;
            }

            if ((bool)value)
            {
                return parameter;
            }
            else
            {
                return DependencyProperty.UnsetValue;
            }
        }
    }
}
